package org.example.state;

import org.example.model.FarmMachine;
import org.example.strategy.NormalPathStrategy;
import org.example.strategy.PathPlanningContext;
import org.example.strategy.WetlandPathStrategy;

public class WorkingState implements MachineState {
    private PathPlanningContext pathPlanningContext;

    public WorkingState() {
        pathPlanningContext = new PathPlanningContext();
        pathPlanningContext.setStrategy(new NormalPathStrategy());
    }

    @Override
    public String getStateName() {
        return "工作状态";
    }

    @Override
    public void handleEvent(FarmMachine machine, String event) {
        switch (event) {
            case "任务完成":
                machine.setState(new IdleState());
                System.out.println(machine.getType() + "[" + machine.getId() + "] 从工作状态切换到闲置状态");
                break;
            case "检测到障碍物":
                machine.setState(new AvoidingState());
                System.out.println(machine.getType() + "[" + machine.getId() + "] 从工作状态切换到避障状态");
                break;
            case "检测到故障":
                machine.setState(new FaultState());
                System.out.println(machine.getType() + "[" + machine.getId() + "] 从工作状态切换到故障状态");
                break;
            case "土壤湿度过高":
                pathPlanningContext.setStrategy(new WetlandPathStrategy());
                System.out.println(machine.getType() + "[" + machine.getId() + "] 调整路径规划策略以应对湿地区域");
                break;
        }
    }

    @Override
    public void execute(FarmMachine machine) {
        machine.setWorking(true);
        machine.performTask();
        pathPlanningContext.planPath(machine);

        double fuel = machine.getFuel();
        fuel -= 5.0;
        machine.setFuel(fuel);
        System.out.println(machine.getType() + "[" + machine.getId() + "] 剩余燃油: " + fuel + "%");
    }
}